package com.loadmore.oneday

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.FrameLayout
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.loadmore.common.auth.LoginActivity
import com.loadmore.common.base.BaseActivity
import com.loadmore.common.entity.TabEntity
import com.loadmore.common.model.MessageEvent
import com.loadmore.common.router.RouterPath
import com.loadmore.oneday.constants.Actions
import com.loadmore.oneday.fragment.HomeFragment
import com.loadmore.oneday.fragment.MineFragment
import com.loadmore.oneday.fragment.PlanFragment
import com.loadmore.schedule.util.ScheduleManager
import com.loadmore.stepcount.StepAlarmManager
import com.loadmore.stepcount.StepCountService
import com.luck.picture.lib.tools.ScreenUtils
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*


@Route(path = RouterPath.Main)
class MainActivity : BaseActivity() {


    private val mTitles = arrayOf("首页", "消息", "我的")
    private val selectIds = intArrayOf(R.mipmap.icon_home, R.mipmap.icon_message, R.mipmap.icon_mine)
    private val unselectIds = intArrayOf(R.mipmap.icon_home, R.mipmap.icon_message, R.mipmap.icon_mine)
    private val tabEntities = ArrayList<CustomTabEntity>()

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if (!TextUtils.isEmpty(intent?.action)) {
            when(intent?.action) {
                Actions.goLogin->{
                    startActivity(Intent(application,LoginActivity::class.java))
                    finish()
                }
            }
        }

    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
        setContentView(R.layout.activity_main)
        hideActionBar()
        setSwipeBackEnable(false)
        //actionBar.setCenterTitle("oneday")
        val bundle = Bundle()
        bundle.putBoolean("showNavigationIcon",false)
        var homeFragment1 = HomeFragment.newInstance(bundle)
        var homeFragment2 = PlanFragment.newInstance(bundle)
        var homeFragment3 = MineFragment.newInstance(bundle)
        val fragments = ArrayList<androidx.fragment.app.Fragment>()
        fragments.add(homeFragment1)
        fragments.add(homeFragment2)
        fragments.add(homeFragment3)
        viewpager.offscreenPageLimit = 4
        var layoutParams:FrameLayout.LayoutParams = viewpager.layoutParams as FrameLayout.LayoutParams
        layoutParams.bottomMargin = ScreenUtils.dip2px(this,50f)
        viewpager.adapter = object :androidx.fragment.app.FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): androidx.fragment.app.Fragment {
                return fragments[position]
            }

            override fun getCount(): Int {
                return fragments.size
            }

        }


        for (i in mTitles.indices) {
            var tabEntity = TabEntity(mTitles[i],selectIds[i],unselectIds[i])
            tabEntities.add(tabEntity)
        }

        tablayout.setTabData(tabEntities)

        viewpager.addOnPageChangeListener(object :androidx.viewpager.widget.ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                tablayout.currentTab = position
            }

        })
        tablayout.setOnTabSelectListener(object :OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                viewpager.setCurrentItem(position,false)
            }

            override fun onTabReselect(position: Int) {
            }

        })
//        web.setOnClickListener {
//            var bundle = Bundle()
//            bundle.putString("url","http://loadmore.cn/pages/route.html")
//            ARouter.getInstance().build(RouterPath.Web).with(bundle).navigation()
//        }

    }

    private fun init() {
        initSystemService()
        ScheduleManager.init(this)
    }
    private fun initSystemService() {
        StepAlarmManager.init(this)
        val intent = Intent(this,StepCountService::class.java)
        startService(intent)
    }

    override fun onMessageEvent(event: MessageEvent) {
        super.onMessageEvent(event)

    }

    override fun onBackPressed() {
        if (checkPress()) {
            super.onBackPressed()
        }

    }

    private var millisecond:Long = 0

    private fun checkPress() :Boolean {
        val currentTime:Long = System.currentTimeMillis()
        val elapse:Long = currentTime - millisecond
        if (elapse < 1600) {
            return true
        }
        Toast.makeText(this,"再按一次退出应用",Toast.LENGTH_SHORT).show()
        millisecond = currentTime
        return false
    }


}
